<template>
  <div class="AmapDrag-wrap" :style="{ width: Width +'px',height:Height +'px' }">
    <div class="m-map">
      <div class="search">
        <el-input v-model="searchKey" placeholder="请输入内容" @change="handleSelect"></el-input>
        <div id="js-result" v-show="pois" class="result">
          <el-scrollbar :native="false" :noresize="false" class="list">
            <!-- 图标项 -->
            <div class="item" v-for="(item,index) in pois" @click="selectItem(item)">
              <div class="map">
              <el-row :gutter="5">
                <el-col :span="3">
                  <div class="grid-content bg-purple map-icon"><i class="fas fa-location-arrow"></i></div>
                </el-col>
                <el-col :span="21">
                  <div class="grid-content bg-purple-light">
                    <p class='map-text'>{{item.name}}</p>
                    <p class='map-text'>{{item.address}}</p>
                  </div>
                </el-col>
              </el-row>
              </div>
            </div>
          </el-scrollbar>
        </div>
      </div>
      <div
        id="js-container"
        class="map"
        :style="{ width: Width +'px',height:Height +'px' }"
      >正在加载数据 ...</div>
    </div>
  </div>
</template>

<script>
import remoteLoad from "../utils/remoteLoad.js";
import { MapKey, MapCityName } from "../config/mapConfig";
export default {
  name: "AmapDrag",
  props: {
    lat: String,
    lng: String,
    Height: {
      type: Number,
      default: 600
    },
    Width: {
      type: Number,
      default: 800
    }
  },
  data() {
    return {
      searchKey: "",
      querySearch: null,
      dragStatus: false,
      AMapUI: null,
      AMap: null,
      pois: null,
      parmas:{
        lat:null,
        lng:null
      }
    };
  },
  watch: {
    searchKey() {
      if (this.searchKey === "") {
        this.placeSearch.clear();
      }
    }
  },
  methods: {
    // 搜索
    handleSelect(name) {
      let _this = this;
      let data = this.placeSearch.search(name, function(status, result) {
        if (status === "complete") {
          _this.pois = result.poiList.pois;
        }
      });
    },
    selectItem(item) {
      if (item.location) {
        this.parmas.lat = item.location.lat;
        this.parmas.lng = item.location.lng;
      } else {
        this.parmas.lat = item.position.lat;
        this.parmas.lng = item.position.lng;
      }
      this.initMap();
    },
    // 实例化地图
    initMap() {
      // 加载PositionPicker，loadUI的路径参数为模块名中 'ui/' 之后的部分
      let AMapUI = (this.AMapUI = window.AMapUI);
      let AMap = (this.AMap = window.AMap);
      AMapUI.loadUI(["misc/PositionPicker"], PositionPicker => {
        let mapConfig = {
          zoom: 16
          //cityName: MapCityName,
        };
        if(this.parmas.lat && this.parmas.lng){
          mapConfig.center = [this.parmas.lng, this.parmas.lat];
        }else{
          if (this.lat && this.lng) {
            mapConfig.center = [this.lng, this.lat];
          }
        }
        

        let map = new AMap.Map("js-container", mapConfig);
        //加载地图搜索插件
        AMap.service("AMap.PlaceSearch", () => {
          this.placeSearch = new AMap.PlaceSearch({
            city: "全国"
          });
        });
        //启用工具条
        AMap.plugin(["AMap.ToolBar"], function() {
          map.addControl(
            new AMap.ToolBar({
              position: "RB"
            })
          );
        });
        // 创建地图拖拽
        let positionPicker = new PositionPicker({
          mode: "dragMap", // 设定为拖拽地图模式，可选'dragMap'、'dragMarker'，默认为'dragMap'
          map: map // 依赖地图对象
        });
        // 拖拽完成发送自定义 drag 事件
        positionPicker.on("success", positionResult => {
          // 过滤掉初始化地图后的第一次默认拖放
          if (!this.dragStatus) {
            this.dragStatus = true;
          } else {
            this.$emit("drag", positionResult);
          }
        });
        // 启动拖放
        positionPicker.start();
      });
    }
  },
  async created() {
    // 已载入高德地图API，则直接初始化地图
    if (window.AMap && window.AMapUI) {
      this.initMap();
      // 未载入高德地图API，则先载入API再初始化
    } else {
      await remoteLoad(`http://webapi.amap.com/maps?v=1.3&key=${MapKey}`);
      await remoteLoad("http://webapi.amap.com/ui/1.0/main.js");
      this.initMap();
    }
  }
};
</script>
<style lang="less">
.m-map {
  min-width: 500px;
  min-height: 300px;
  position: relative;
}
.m-map .map {
  width: 100%;
  height: 100%;
}
.m-map .search {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 285px;
  z-index: 1;
}
.m-map .result {
  max-height: 300px;
  overflow: hidden;
  margin-top: 10px;
  background: #fff;
  padding: 10px;
  .el-scrollbar__wrap {
    overflow-x: hidden!important;
    .item {
      cursor:pointer;
      border-bottom: 1px solid #eee;
      .map .el-row {
          padding: 10px;
      }
      &:hover {
        background: #ecf5ff;
      }
    }
  }
  .list {
    height: 280px;
  }
}
.map .el-row {
  padding: 5px 0 10px;
}
.map-icon {
  text-align: center;
  line-height: 3rem;
  font-size: 1rem;
}
.map-text {
  line-height: 100%;
}
.map-text:nth-child(1) {
  font-size: 16px;
  margin:0 0 5px 0;
  font-weight: 700;
}
.map-text:nth-child(2) {
  font-size: 12px;
  line-height: 16px;
  margin: 0
}
</style>
